﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DatabaseUserTest.Controller;
using DatabaseUserTest.DB;

namespace DatabaseUserTest.Model
{
    class NodeAdd
    {
        Graph graph = new Graph();
        
        public Graph AddNode(int check, DateTime date, int seats, bool firstClass)
        {
            
            DestinationCTRL destctrl = new DestinationCTRL();
            DBRoute findrouts =new DBRoute();
            PlaneCTRL planeCtrl = new PlaneCTRL();
            List<Plane> planeList = planeCtrl.FindAllPlanes();
            List<Destination> destList = destctrl.FindAllDestinations();
            List<Route> route = findrouts.FindAllRoutes();

            foreach (Destination item in destList)
            {
                graph.AddNode(item.Name);
            }
            if (check == 1)
            {
                if(firstClass)
                {
                    foreach (Plane item in planeList)
                    {
                        if (date.Date <= item.Departure.Date && seats <= planeCtrl.FindAvailableFirstClassSeats(item.PlaneId) && item.Departure.Date.AddHours(24) > date.Date)
                        {
                            graph.AddDirectedEdge(item.Route.FromDestination, item.Route.ToDestination, item.Route.Price);  
                        }
                    }
                }
                else
                {
                    foreach (Plane item in planeList)
                    {
                        if (date.Date <= item.Departure.Date && seats <= planeCtrl.FindAvailableNormalSeats(item.PlaneId) && item.Departure.Date.AddHours(24) > date.Date)
                        {
                            graph.AddDirectedEdge(item.Route.FromDestination, item.Route.ToDestination, item.Route.Price);
                        }
                    }
                }
            }
            else
            {
                if (firstClass)
                {
                    foreach (Plane item in planeList)
                    {
                        if (date.Date <= item.Departure.Date && seats <= planeCtrl.FindAvailableFirstClassSeats(item.PlaneId) && item.Departure.Date.AddHours(24) > date.Date)
                        {
                            graph.AddDirectedEdge(item.Route.FromDestination, item.Route.ToDestination, item.Route.Time);
                        }
                    }
                }
                else
                {
                    foreach (Plane item in planeList)
                    {
                        if (date.Date <= item.Departure.Date && seats <= planeCtrl.FindAvailableNormalSeats(item.PlaneId) && item.Departure.Date.AddHours(24) > date.Date)
                        {
                            graph.AddDirectedEdge(item.Route.FromDestination, item.Route.ToDestination, item.Route.Time);
                        }
                    }
                }
            }
            return graph;
        }
    }
}
